package it.unical.mat.aec.gui;

import it.unical.mat.aec.manageResult.ManageResultFacade;
import it.unical.mat.aec.model.Test;
import it.unical.mat.aec.model.Traccia;
import it.unical.mat.aec.persistence.PersistanceException;
import it.unical.mat.aec.processTests.AssociazioneTracciaException;
import it.unical.mat.aec.processTests.FileNonValidoException;
import it.unical.mat.aec.processTests.LettoreFileTestException;
import it.unical.mat.aec.processTests.ProcessTestsFacade;
import it.unical.mat.aec.processTests.TipoInputFile;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import com.sun.crypto.provider.ARCFOURCipher;

public class MainFrame extends JFrame {
	
	public static void main(String[] args) {
		MainFrame.getInstance();
	}
	
	private static MainFrame mainFrame;
	
	private JPanel contenentPanel;
	private JPanel mainPanel;

	public static MainFrame getInstance(){
		if(mainFrame==null)
			mainFrame=new MainFrame();
		return mainFrame;
	}
	
	private void changeLookFell(MainFrame mainFrame) {
		try {
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
		        if ("Windows".equals(info.getName())) {
		            UIManager.setLookAndFeel(info.getClassName());
		            break;
		        }
		    }
		} catch (Exception e) {
			try {
				UIManager.setLookAndFeel(UIManager
						.getSystemLookAndFeelClassName());
			} catch (Exception e2) {
				JOptionPane.showMessageDialog(null,
						"Impossibile caricare lookAndFeel", "Errore",
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	
	private MainFrame() {
		super("Automatic Exam Correction - AEC");
		changeLookFell(mainFrame);
		contenentPanel = new JPanel(new BorderLayout());
		mainPanel=new JPanel(new BorderLayout());
		this.setSize(900, 600);
		this.setLocation(200, 50);
		this.setIconImage(Toolkit.getDefaultToolkit().getImage("images/icon/iconAEC.png"));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		startComponent();
		startPanel();
		this.setVisible(true);
	}
	
	private void startComponent(){
		mainPanel.add(contenentPanel,BorderLayout.CENTER);
		JMenuBar menuBar=new JMenuBar();
		JMenu menuAEC=new JMenu("AEC");
		JMenu menuAE=new JMenu("Acquisisci Esami");
		JMenuItem menuItemAEXML=new JMenuItem("XML");
		JMenuItem menuItemMR=new JMenuItem("Modifica Risultati");
		JMenuItem menuItemS=new JMenuItem("Visualizza Statistiche");
		menuAE.add(menuItemAEXML);
		menuAEC.add(menuAE);
		menuAEC.add(menuItemMR);
		menuAEC.add(menuItemS);
		menuBar.add(menuAEC);
		menuItemAEXML.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ProcessTestsFacade facade=ProcessTestsFacade.getInstance();
				File files[]=CaricaFile.getFile(MainFrame.this.contenentPanel);
				if(files!=null){
						try {
							facade.getTests().clear();
							facade.acquisciEsami(files, TipoInputFile.XML);
						} catch (LettoreFileTestException e) {
							int response=JOptionPane.showConfirmDialog(MainFrame.this,"Error :"+e.getMessage()+"\n\nVuoi continuare ?");
							if(response==JOptionPane.NO_OPTION)
								return ;
						} catch (FileNonValidoException e) {
							JOptionPane.showMessageDialog(MainFrame.this, e.getMessage(), "Errore", JOptionPane.ERROR_MESSAGE);
							return ;
						}
						try {
							Traccia tracciaDaAssociare = facade.getTracciaDaAssociare();
							int response=JOptionPane.showConfirmDialog(MainFrame.this, "Traccia da associare :\n"+tracciaDaAssociare+"\n\nPremere si per confermare , no per scegliere una traccia.");
							if(response==JOptionPane.OK_OPTION){
								try {
									facade.associaTraccia(tracciaDaAssociare.getId());
									correzionePanel();
								} catch (PersistanceException e) {
									JOptionPane.showMessageDialog(MainFrame.this, e.getMessage(), "Errore", JOptionPane.ERROR_MESSAGE);
								}
							}
							else if(response==JOptionPane.NO_OPTION)
								visualizzaTraccePanel();
						} catch (AssociazioneTracciaException e) {
							JOptionPane.showMessageDialog(MainFrame.this, e.getMessage(), "Errore", JOptionPane.ERROR_MESSAGE);
							visualizzaTraccePanel();
						}
				}
			}
		});
		menuItemMR.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				File path[]=CaricaFile.getFile(MainFrame.this.contenentPanel);
				if(path!=null)
					if(path.length==1){
						ManageResultFacade.getInstance().getTests().clear();
						if(ManageResultFacade.getInstance().caricaTests(path[0].getPath())){
							JOptionPane.showMessageDialog(MainFrame.this,"Caricamento avvenuto con successo", "Informazione", JOptionPane.INFORMATION_MESSAGE);
							modificaPanel();
						}
						else
							JOptionPane.showMessageDialog(MainFrame.this, "Errore durante il caricamento , file non supportato o corrotto", "Errore", JOptionPane.ERROR_MESSAGE);
					}else
						JOptionPane.showMessageDialog(MainFrame.this,"Seleziona un solo file", "Informazione", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		mainPanel.add(menuBar,BorderLayout.NORTH);
		add(mainPanel);
	}
	
	
	protected void switchTO(final JPanel panel) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				contenentPanel.removeAll();
				contenentPanel.add(panel, BorderLayout.CENTER);
				contenentPanel.updateUI();
				panel.requestFocus();
			}
		});
	}
	
	public void startPanel(){
		JPanel panel=new JPanel();
		switchTO(panel);
	}
	
	public void visualizzaTraccePanel(){
		VisualizzaTraccePanel v=new VisualizzaTraccePanel();
		switchTO(v);
	}
	
	public void correzionePanel(){
		CorrezionePanel correzionePanel=new CorrezionePanel();
		switchTO(correzionePanel);
	}
	

	public void VisualizzaTestPanel(int IdTest){
		VisualizzaTestPanel visualizzaTestPanel=new VisualizzaTestPanel(IdTest);
		switchTO(visualizzaTestPanel);
	}
	public void modificaPanel() {
		ModificaPanel modificaPanel=new ModificaPanel();
		switchTO(modificaPanel);
		
	}

}
